/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CajeroVenta.java
 *
 * Created on 7/11/2009, 07:29:21 PM
 */

package scapi.gui;

import com.jidesoft.swing.AutoCompletionComboBox;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import scapi.entidades.Direccion;
import scapi.entidades.Persona;
import scapi.entidades.Producto;
import scapi.servidor.servidores.ServidorAProducto;
import scapi.servidor.servidores.ServidorAVentas;
import scapi.servidor.servidores.ServidorReporte;
import scapi.utilerias.EValidacion;
import scapi.utilerias.ExcepcionValidacion;
import scapi.utilerias.ModeloTablaProducto;
import scapi.utilerias.ServidorFactory;

/**
 *
 * @author emmanuel
 */
public class CajeroVenta extends javax.swing.JPanel {
    private ServidorAProducto servPro;
    private ArrayList<Producto> productoF;
    private ServidorAVentas servVentas;
    private ServidorReporte reportes;


    /** Creates new form CajeroVenta */
    public CajeroVenta() {
        initComponents();
        try {
            servPro = (ServidorAProducto) ServidorFactory.obtenerServidor(ServidorFactory.ServidorProducto);
            reportes = (ServidorReporte) ServidorFactory.obtenerServidor(ServidorFactory.ServidorReportes);
        } catch (RemoteException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        }

        Producto[] productos = new Producto[0];
        try {
            productos = servPro.consultaTodo(1);
            System.out.print(productos.length);
            CBCVnompro.setModel(new DefaultComboBoxModel(productos));
        } catch (RemoteException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        }

        productoF = new ArrayList<Producto>();
        try {
            servVentas = (ServidorAVentas) ServidorFactory.obtenerServidor(ServidorFactory.ServidorVentas);
        } catch (RemoteException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(CajeroVenta.class.getName()).log(Level.SEVERE, null, ex);
        }

        

        




    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BGCVsexo = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        JBCVaceptar = new javax.swing.JButton();
        JBCVcancelar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        JPCVproducto = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        CBCVnompro = new AutoCompletionComboBox();
        jLabel20 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        TACVdesc = new javax.swing.JTextArea();
        JBCVagregar = new javax.swing.JButton();
        JBCVborrar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        JPCVcliente = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        TFCVapat = new javax.swing.JTextField();
        TFCVamat = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        TFCVrfc = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        TFCVcalle = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        TFCVciudad = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        TFCVnumExt = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        TFCVnumInt = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        TFCVcolonia = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        TFCVlada = new javax.swing.JTextField();
        TFCVtelefono = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        CBCVestado = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        TFCVcp = new javax.swing.JTextField();
        jSeparator10 = new javax.swing.JSeparator();
        DCCVfecnac = new com.toedter.calendar.JDateChooser();
        labFechaNac = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        RBCVhombre = new javax.swing.JRadioButton();
        RBCVmujer = new javax.swing.JRadioButton();
        TFCVemail = new javax.swing.JTextField();
        CBCVnomcte = new javax.swing.JTextField();
        LCVtotal = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        LCVnumero = new javax.swing.JLabel();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();

        setLayout(new java.awt.BorderLayout());

        JBCVaceptar.setText("Aceptar");
        JBCVaceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBCVaceptarActionPerformed(evt);
            }
        });
        jPanel1.add(JBCVaceptar);

        JBCVcancelar.setText("Cancelar");
        JBCVcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBCVcancelarActionPerformed(evt);
            }
        });
        jPanel1.add(JBCVcancelar);

        add(jPanel1, java.awt.BorderLayout.PAGE_END);

        JPCVproducto.setBorder(javax.swing.BorderFactory.createTitledBorder("Busqueda Producto"));

        jLabel3.setText("Nombre");

        CBCVnompro.setEditable(true);
        CBCVnompro.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                CBCVnomproItemStateChanged(evt);
            }
        });
        CBCVnompro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBCVnomproActionPerformed(evt);
            }
        });
        CBCVnompro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                CBCVnomproKeyTyped(evt);
            }
        });

        jLabel20.setText("Descripcion");

        TACVdesc.setColumns(20);
        TACVdesc.setLineWrap(true);
        TACVdesc.setRows(5);
        jScrollPane2.setViewportView(TACVdesc);

        JBCVagregar.setText("Agregar");
        JBCVagregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBCVagregarActionPerformed(evt);
            }
        });

        JBCVborrar.setText("Borrar");

        javax.swing.GroupLayout JPCVproductoLayout = new javax.swing.GroupLayout(JPCVproducto);
        JPCVproducto.setLayout(JPCVproductoLayout);
        JPCVproductoLayout.setHorizontalGroup(
            JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCVproductoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JPCVproductoLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(JPCVproductoLayout.createSequentialGroup()
                        .addGroup(JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel20)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, JPCVproductoLayout.createSequentialGroup()
                                .addGroup(JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, JPCVproductoLayout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(CBCVnompro, 0, 311, Short.MAX_VALUE))
                                    .addGroup(JPCVproductoLayout.createSequentialGroup()
                                        .addComponent(JBCVagregar)
                                        .addGap(18, 18, 18)
                                        .addComponent(JBCVborrar)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGap(19, 19, 19))))
        );
        JPCVproductoLayout.setVerticalGroup(
            JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCVproductoLayout.createSequentialGroup()
                .addGroup(JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(CBCVnompro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel20)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(JPCVproductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JBCVagregar)
                    .addComponent(JBCVborrar))
                .addContainerGap())
        );

        jLabel6.setText("Fecha:");

        jTable1.setModel(new ModeloTablaProducto());
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);

        JPCVcliente.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Clientes"));

        jLabel4.setText("Nombre");

        jLabel5.setText("Apellidos");

        jLabel7.setText("RFC");

        jLabel8.setText("Dirección");

        TFCVcalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFCVcalleActionPerformed(evt);
            }
        });

        jLabel12.setText("Calle");

        jLabel13.setText("Ciudad");

        TFCVciudad.setFont(new java.awt.Font("Tahoma", 2, 11));
        TFCVciudad.setForeground(new java.awt.Color(153, 153, 153));
        TFCVciudad.setText("Delegacion o Municipio");
        TFCVciudad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                TFCVciudadFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                TFCVciudadFocusLost(evt);
            }
        });

        jLabel14.setText("No. Ext");

        jLabel15.setText("No. Int");

        jLabel16.setText("Colonia");

        jLabel17.setText("Teléfono");

        jLabel18.setText("Estado");

        CBCVestado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccionar--", "Aguascalientes", "Baja California", "Baja California Sur", "Campeche", "Chiapas", "Chihuahua", "Coahuila", "Colima", "Distrito Federal", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco", "México", "Michoacán", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro", "Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala", "Veracruz", "Yucatán", "Zacatecas" }));

        jLabel19.setText("C.P.");

        labFechaNac.setText("Nacimiento");

        jLabel1.setText("Sexo");

        BGCVsexo.add(RBCVhombre);
        RBCVhombre.setText("H");

        BGCVsexo.add(RBCVmujer);
        RBCVmujer.setText("M");

        TFCVemail.setFont(new java.awt.Font("Tahoma", 2, 11));
        TFCVemail.setForeground(new java.awt.Color(153, 153, 153));
        TFCVemail.setText("Correo electronico");
        TFCVemail.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                TFCVemailFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                TFCVemailFocusLost(evt);
            }
        });

        javax.swing.GroupLayout JPCVclienteLayout = new javax.swing.GroupLayout(JPCVcliente);
        JPCVcliente.setLayout(JPCVclienteLayout);
        JPCVclienteLayout.setHorizontalGroup(
            JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCVclienteLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator10, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
                    .addGroup(JPCVclienteLayout.createSequentialGroup()
                        .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(JPCVclienteLayout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(CBCVnomcte, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE))
                            .addGroup(JPCVclienteLayout.createSequentialGroup()
                                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel7))
                                .addGap(10, 10, 10)
                                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(JPCVclienteLayout.createSequentialGroup()
                                        .addComponent(TFCVapat, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(TFCVamat, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                                    .addComponent(TFCVrfc, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)))
                            .addComponent(jLabel8)
                            .addGroup(JPCVclienteLayout.createSequentialGroup()
                                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel16)
                                    .addComponent(jLabel12))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(TFCVcolonia, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                                    .addComponent(TFCVcalle, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(JPCVclienteLayout.createSequentialGroup()
                                        .addComponent(TFCVnumExt, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel15)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(TFCVnumInt, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                                    .addComponent(TFCVciudad, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)))
                            .addGroup(JPCVclienteLayout.createSequentialGroup()
                                .addComponent(jLabel18)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(CBCVestado, 0, 287, Short.MAX_VALUE)
                                .addGap(49, 49, 49)
                                .addComponent(jLabel19)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(TFCVcp, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(JPCVclienteLayout.createSequentialGroup()
                                .addComponent(jLabel17)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(TFCVlada, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(TFCVtelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(25, 25, 25)
                                .addComponent(TFCVemail, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(JPCVclienteLayout.createSequentialGroup()
                        .addComponent(labFechaNac)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DCCVfecnac, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(RBCVhombre)
                        .addGap(18, 18, 18)
                        .addComponent(RBCVmujer)
                        .addGap(48, 48, 48))))
        );
        JPCVclienteLayout.setVerticalGroup(
            JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCVclienteLayout.createSequentialGroup()
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(CBCVnomcte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(TFCVamat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TFCVapat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(TFCVrfc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator10, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(TFCVnumExt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(TFCVnumInt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TFCVcalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(TFCVciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TFCVcolonia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(CBCVestado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19)
                    .addComponent(TFCVcp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(TFCVlada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TFCVtelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TFCVemail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JPCVclienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                        .addComponent(RBCVhombre)
                        .addComponent(RBCVmujer))
                    .addComponent(labFechaNac, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                    .addComponent(DCCVfecnac, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE))
                .addContainerGap())
        );

        LCVtotal.setFont(new java.awt.Font("Tahoma", 1, 14));
        LCVtotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LCVtotal.setText("$0.00");

        jLabel10.setText("Numero:");

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel21.setText("Total:");

        LCVnumero.setFont(new java.awt.Font("Tahoma", 1, 11));
        LCVnumero.setForeground(new java.awt.Color(102, 0, 0));
        LCVnumero.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LCVnumero.setText("00000000000");

        jDateChooser1.setDate(new Date());
        jDateChooser1.setEnabled(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(JPCVcliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JPCVproducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 938, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel21)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(LCVtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(429, 429, 429))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LCVnumero, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(478, 478, 478)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(LCVnumero)
                            .addComponent(jLabel10))
                        .addGap(6, 6, 6))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel6)
                        .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(JPCVproducto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(JPCVcliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LCVtotal)
                    .addComponent(jLabel21))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel4, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void TFCVcalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFCVcalleActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_TFCVcalleActionPerformed

    private void JBCVaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBCVaceptarActionPerformed


        
        try {
            String nomCte = EValidacion.cadenaCorrecta((String) CBCVnomcte.getText(), "Nombre Cliente");
            String aPat = EValidacion.cadenaCorrecta(TFCVapat.getText(), "Apellido Paterno");
            String aMat = EValidacion.cadenaCorrecta(TFCVamat.getText(), "Apellido Materno");
            String RFC = EValidacion.RFC(TFCVrfc.getText(), "RFC");
            String calle = EValidacion.campoVacio(TFCVcalle.getText(), "Calle");
            int nExt = EValidacion.entero(TFCVnumExt.getText(), "Numero Exterior");
            String nInt = TFCVnumInt.getText();
            String colonia = EValidacion.cadenaCorrecta(TFCVcolonia.getText(), "Colonia");
            String ciudad = EValidacion.cadenaCorrecta(TFCVciudad.getText(), "Ciudad");
	    String estado;
            if (CBCVestado.getSelectedIndex()==0)
                throw new ExcepcionValidacion("Debe elegir un estado");
	    else
		estado = EValidacion.cadenaCorrecta((String)CBCVestado.getSelectedItem(), "Estado");
            int cp = EValidacion.CP(TFCVcp.getText(), "Codigo Postal");
            int lada = EValidacion.LADA(TFCVlada.getText(), "LADA");
            int telefono = EValidacion.entero(TFCVtelefono.getText(), "Telefono");
	    String email = EValidacion.email(TFCVemail.getText(), "Correo electronico");
	    char sexo;
	    if (!RBCVhombre.isSelected() && !RBCVmujer.isSelected())
		throw new ExcepcionValidacion("Debe seleccionar un genero");
	    else if (RBCVhombre.isSelected())
		sexo = 'H';
	    else
		sexo = 'M';
	    Date fecnac = EValidacion.fecha(DCCVfecnac.getDate());
	    Direccion direccion = new Direccion(calle, nInt, nExt, colonia, cp, ciudad, estado, "Mexico");
            Persona persona = new Persona(0, nomCte, aPat, aMat, fecnac, sexo, lada, telefono, direccion, email, RFC);

            Iterator itr = this.productoF.iterator();
            Producto [] prod = new Producto[productoF.size()];
            int i = 0;
            while (itr.hasNext()) {
                Producto producto = (Producto) itr.next();
                prod[i] = producto;
                i++;

            }
            servVentas.realizarVenta(persona, prod);

            JOptionPane.showMessageDialog(this, "Venta Realizada Correctamente");

            limpiarForm();

            HashMap<String, String> hm = new HashMap<String, String>();

            int id = servVentas.ultimaVenta();


            hm.put("id_per", ""+id);

            JasperPrint print = reportes.getReporte("Factura.jasper",hm );
            JasperViewer.viewReport(print,false);





        } catch (ExcepcionValidacion e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        } catch(NullPointerException e){
            JOptionPane.showMessageDialog(this, "[Nombre Cliente] Debe escribir algo", "ERROR", JOptionPane.ERROR_MESSAGE);
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }//GEN-LAST:event_JBCVaceptarActionPerformed

    private void JBCVagregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBCVagregarActionPerformed

        try {
           Producto producto = (Producto) CBCVnompro.getSelectedItem();

           if(producto == null)
               throw new ExcepcionValidacion("Deve seleccionar producto");
           
           productoF.add(producto);

           jTable1.setModel(new ModeloTablaProducto(this.productoF));

           LCVtotal.setText(""+calcularTotal());




        } catch (ExcepcionValidacion e) {
            JOptionPane.showMessageDialog(this, e.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        } catch(NullPointerException e){
            JOptionPane.showMessageDialog(this, "[Nombre Producto] Debe escribir algo", "ERROR", JOptionPane.ERROR_MESSAGE);

        }
}//GEN-LAST:event_JBCVagregarActionPerformed

    private void TFCVciudadFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TFCVciudadFocusGained

	if (TFCVciudad.getText().equalsIgnoreCase("Delegacion o Municipio")) {
	    TFCVciudad.setFont(null);
	    TFCVciudad.setForeground(null);
	    TFCVciudad.setText("");
	}
    }//GEN-LAST:event_TFCVciudadFocusGained

    private void TFCVciudadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TFCVciudadFocusLost
	if (TFCVciudad.getText().isEmpty()){
	    TFCVciudad.setFont(new java.awt.Font("Tahoma", 2, 11));
            TFCVciudad.setForeground(new java.awt.Color(153, 153, 153));
	    TFCVciudad.setText("Delegacion o Municipio");
	}
    }//GEN-LAST:event_TFCVciudadFocusLost

    private void TFCVemailFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TFCVemailFocusGained
	if (TFCVemail.getText().equalsIgnoreCase("Correo electronico")) {
	    TFCVemail.setFont(null);
	    TFCVemail.setForeground(null);
	    TFCVemail.setText("");
	}
    }//GEN-LAST:event_TFCVemailFocusGained

    private void TFCVemailFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_TFCVemailFocusLost
	if (TFCVemail.getText().isEmpty()) {
            TFCVemail.setFont(new java.awt.Font("Tahoma", 2, 11));
            TFCVemail.setForeground(new java.awt.Color(153, 153, 153));
	    TFCVemail.setText("Correo electronico");
	}
    }//GEN-LAST:event_TFCVemailFocusLost

    private void CBCVnomproKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CBCVnomproKeyTyped

        int code = evt.getKeyCode();
        if(code == KeyEvent.VK_ENTER){
            JOptionPane.showMessageDialog(this, "OK");
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_CBCVnomproKeyTyped

    private void CBCVnomproItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_CBCVnomproItemStateChanged
        llenarDesc();

        // TODO add your handling code here:
    }//GEN-LAST:event_CBCVnomproItemStateChanged

    private void CBCVnomproActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBCVnomproActionPerformed

        llenarDesc();
        // TODO add your handling code here:
    }//GEN-LAST:event_CBCVnomproActionPerformed

    private void JBCVcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBCVcancelarActionPerformed

        limpiarForm();
        // TODO add your handling code here:
    }//GEN-LAST:event_JBCVcancelarActionPerformed

    private void llenarDesc(){
       Producto prod = (Producto) CBCVnompro.getSelectedItem();
       TACVdesc.setText(prod.getDescripcion());

    }

    private void limpiarPanel(JPanel panel){
        Component[] comp = panel.getComponents();
        for (int i = 0; i < comp.length; i++) {
            Component component = comp[i];
            if(component instanceof JTextField){
                JTextField componente = (JTextField) component;
                componente.setText("");
            }else if(component instanceof JComboBox){
                JComboBox comp2 = (JComboBox) component;
                comp2.setSelectedIndex(0);

            }else if(component instanceof JCheckBox){
                JCheckBox box = (JCheckBox) component;
                box.setSelected(false);
            }

        }

    }

    private float calcularTotal(){
        float total = 0;
        Iterator itr = productoF.iterator();
        while (itr.hasNext()) {
            Producto prod = (Producto) itr.next();
            total += prod.getPrecio();

        }
        return total;
    }

    private void limpiarForm(){
      limpiarPanel(JPCVcliente);
      
      jTable1.setModel(new ModeloTablaProducto());
      TACVdesc.setText("");

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup BGCVsexo;
    private javax.swing.JComboBox CBCVestado;
    private javax.swing.JTextField CBCVnomcte;
    private javax.swing.JComboBox CBCVnompro;
    private com.toedter.calendar.JDateChooser DCCVfecnac;
    private javax.swing.JButton JBCVaceptar;
    private javax.swing.JButton JBCVagregar;
    private javax.swing.JButton JBCVborrar;
    private javax.swing.JButton JBCVcancelar;
    private javax.swing.JPanel JPCVcliente;
    private javax.swing.JPanel JPCVproducto;
    private javax.swing.JLabel LCVnumero;
    private javax.swing.JLabel LCVtotal;
    private javax.swing.JRadioButton RBCVhombre;
    private javax.swing.JRadioButton RBCVmujer;
    private javax.swing.JTextArea TACVdesc;
    private javax.swing.JTextField TFCVamat;
    private javax.swing.JTextField TFCVapat;
    private javax.swing.JTextField TFCVcalle;
    private javax.swing.JTextField TFCVciudad;
    private javax.swing.JTextField TFCVcolonia;
    private javax.swing.JTextField TFCVcp;
    private javax.swing.JTextField TFCVemail;
    private javax.swing.JTextField TFCVlada;
    private javax.swing.JTextField TFCVnumExt;
    private javax.swing.JTextField TFCVnumInt;
    private javax.swing.JTextField TFCVrfc;
    private javax.swing.JTextField TFCVtelefono;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator10;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel labFechaNac;
    // End of variables declaration//GEN-END:variables

}
